#include <iostream>
#include <unistd.h>
#include <thread>
#include <mutex>
#include <condition_variable>

class Foo
{
public:
    Foo()
        : _isonedone(false),
          _istwodone(false)
    {
    }
    void One()
    {
        std::unique_lock<std::mutex> lock(_mutex);
        std::cout << "One" << std::endl;
        _isonedone = true;
        _cv1.notify_one(); // 唤醒在_cv条件变量下等待的线程
    }
    void Two()
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _cv1.wait(lock, [this] // 释放锁,并阻塞等待通知,需要满足_isonedone为true
                  { return _isonedone; });
        std::cout << "Two" << std::endl;
        _istwodone=true;
        _cv2.notify_one();
    }
    void Three()
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _cv2.wait(lock, [this]
                  { return _istwodone; });
        std::cout << "Three" << std::endl;
    }
    ~Foo()
    {
        // std::mutex和std::condition_variable都不需要主动销毁
    }

private:
    std::mutex _mutex;
    std::condition_variable _cv1, _cv2;
    bool _isonedone;
    bool _istwodone;
};
int main()
{
    Foo foo;
    std::thread t1(&Foo::One, &foo); // 不要带括号,这里是函数指针,不是函数调用
    std::thread t2(&Foo::Two, &foo);
    std::thread t3(&Foo::Three, &foo);
    t1.join();
    t2.join();
    t3.join();
    return 0;
}